import { SceneType } from "../../../../../../Core/Module/Entity/SceneType";
import { ErrorCore } from "../../../../../../Core/Module/Network/ErrorCore";
import { SerializeHelper } from "../../../../../../Core/Serialize/SerializeHelper";
import { MessageHandlerAttribute } from "../../../../../../Decorator/Decorator";
import { ETTask } from "../../../../../../Support/Share/Lib/Async/ETTask";
import { C2G_LoginGate, G2C_LoginGate } from "../../../../../Model/Generate/Share/Message/OuterMessage_C_10001";
import { GateSessionKeyComponent } from "../../../../../Model/Server/Demo/Gate/GateSessionKeyComponent";
import { Player } from "../../../../../Model/Server/Demo/Gate/Player";
import { PlayerComponent } from "../../../../../Model/Server/Demo/Gate/PlayerComponent";
import { SessionPlayerComponent } from "../../../../../Model/Server/Demo/Gate/SessionPlayerComponent";
import { MailBoxComponent } from "../../../../../Model/Server/Module/Actor/MailBoxComponent";
import { MailboxType } from "../../../../../Model/Server/Module/Actor/MailboxType";
import { DBManagerComponent } from "../../../../../Model/Server/Module/DB/DBManagerComponent";
import { AMRpcHandler } from "../../../../../Model/Server/Module/Message/AMRpcHandler";
import { ErrorCode } from "../../../../../Model/Share/Module/Message/ErrorCode";
import { Session } from "../../../../../Model/Share/Module/Message/Session";
import { SessionAcceptTimeoutComponent } from "../../../../../Model/Share/Module/Message/SessionAcceptTimeoutComponent";

@MessageHandlerAttribute(SceneType.Gate, C2G_LoginGate, G2C_LoginGate)
export class C2G_LoginGateHandler extends AMRpcHandler<C2G_LoginGate, G2C_LoginGate> {

    protected async Run(session: Session, request: C2G_LoginGate, response: G2C_LoginGate) {
        response = SerializeHelper.PackMessage(G2C_LoginGate, response);
        response.Error = ErrorCode.ERR_Success;

        const scene = session.DomainScene();
        const account = scene.GetComponent<GateSessionKeyComponent>(GateSessionKeyComponent).Get(request.Key);
        if (account == null) {
            response.Error = ErrorCore.ERR_ConnectGateKeyError;
            response.Message = "Gate key验证失败!";
            return;
        }

        session.RemoveComponent<SessionAcceptTimeoutComponent>(SessionAcceptTimeoutComponent);
        const playerComponent = scene.GetComponent<PlayerComponent>(PlayerComponent);
        const player = playerComponent.AddChild<Player, string>(Player, account);
        playerComponent.Add(player);
        session.AddComponent<SessionPlayerComponent>(SessionPlayerComponent).PlayerId = player.Id;
        session.AddComponent<MailBoxComponent, MailboxType>(MailBoxComponent, MailboxType.GateSession);
        response.PlayerId = player.Id;

        const db = await DBManagerComponent.Instance.GetZoneDB(player.DomainZone());
        // await db.InsertBatch<Player>(Player, [player]);
        // const roleInfo = await db.Query<Player>(Player, { Id: 1 });

        // console.log(`C2G_LoginGateHandler`);
        // console.log(request);
        // console.log(response);

        await ETTask.CompletedTask;
    }

}